package com.third.device.mg.serialport.factory.forick.product

import com.third.device.mg.serialport.factory.forick.BaseForickProduct
import com.third.device.mg.serialport.factory.forick.VendorFLKFromPl
import com.third.device.mg.serialport.utils.to2ByteL
import com.third.device.mg.serialport.utils.to4ByteL


/**
 * @Date 2022/4/18
 * Description:
 */
class Panel3Or1 : BaseForickProduct() {

    override fun productDescribe(): String = "弗雷克三选一温控面板--蓝牙版本--佩林协议"

    /**
     * 获取三选一面板当前的状态
     */
    fun getNowState(): ByteArray = byteArrayOf(VendorFLKFromPl.TC_STATUS_GET)

    /**
     * 设置面板模式
     * [model] 01 空调  02 地暖 03 新风
     */
    fun setPanelModel(model: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_WORK_MODE
        VendorFLKFromPl.short2byteBE(model.toShort(), command, 2)
        return command
    }

    /**
     * 空调开关机
     * @param state 0是关机  1是开机
     * @return 空调开关机的指令
     */
    fun setACOnOff(state: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_AC_ONOFF
        VendorFLKFromPl.short2byteBE(state.toShort(), command, 2)
        return command
    }

    /**
     * 空调开机 -- 场景
     */
    fun getACOnOffScene(state: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_AC_ONOFF
        VendorFLKFromPl.short2byteBE(state.toShort(), command, 1)
        return command
    }


    /**
     * 空调模式
     * @param model 00：制冷
     *              01：制热
     *              02：除湿
     *              03：送风
     * @return 空调切换模式的指令
     */
    fun setACModel(model: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_AC_MODE
        VendorFLKFromPl.short2byteBE(model.toShort(), command, 2)
        return command
    }

    /**
     *  设置空调模式--场景
    */
    fun getACModelScene(model: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_AC_MODE
        VendorFLKFromPl.short2byteBE(model.toShort(), command, 1)
        return command
    }


    /**
     * 空调风速
     * [model]0:自动 1:低速 2:中速 3:高速
     */
    fun setACSpeed(model: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_AC_SPEED
        if (model == 0) {
            VendorFLKFromPl.short2byteBE(256, command, 2)
        } else {
            VendorFLKFromPl.short2byteBE(model.toShort(), command, 2)
        }
        return command
    }

    /**
     *  空调风速  -- 场景
     */
    fun getACSpeedScene(model: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_AC_SPEED
        if (model == 0) {
            VendorFLKFromPl.short2byteBE(256, command, 1)
        } else {
            VendorFLKFromPl.short2byteBE(model.toShort(), command, 1)
        }
        return command
    }

    /**
     * 设置空调的制冷/制热的温度
     * [temp] 10℃~32℃之间
     */
    fun setACTemperature(temp: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_AC_TEMPERATURE
        VendorFLKFromPl.short2byteBE((temp * 10).toShort(), command, 2)
        return command
    }

    /**
     * 设置空调的制冷/制热的温度--场景
     */
    fun getACTemperatureScene(temp: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_AC_TEMPERATURE
        VendorFLKFromPl.short2byteBE((temp * 10).toShort(), command, 1)
        return command
    }


    /**
     * 设置空调继电器的控制模式
     * (空调的优先级高) 自控是面板内部逻辑控制开关，被控是通过指令控制开关，不过这个被控你们应该是没做处理的，都是采用我们面板内部逻辑控制
         * @param relay   继电器    1:一路双线阀或一路三线阀（一控）默认
     *                         2:一路三线阀（二控）
     *                         3:两路双线阀或一路三线阀（一控）
     */
    fun setACRelay(relay:Int):ByteArray{
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_AC_RELAY
        command[2] = 0x01
        command[3] = relay.to2ByteL()[0]
        return command
    }

    /**
     * 设置空调继电器的控制模式 -- 场景
     */
    fun getACRelayScene(relay:Int):ByteArray{
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_AC_RELAY
        command[1] = 0x01
        command[2] = relay.to2ByteL()[0]
        return command
    }

    /**
     * 设置空调继电器无效化
     */
    fun setACRelayInvalid():ByteArray{
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_AC_RELAY
        command[2] = 0x00
        command[3] = 0x00
        return command
    }

    /**
     * 设置空调继电器无效化--场景
     */
    fun getACRelayInvalidScene():ByteArray{
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_AC_RELAY
        command[1] = 0x00
        command[2] = 0x00
        return command
    }


    /**
     * 设置地暖开关
     * @param state 0是关机  1是开机
     */
    fun setFHOnOff(state: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_FH_ONOFF
        VendorFLKFromPl.short2byteBE(state.toShort(), command, 2)
        return command
    }

    /**
     * 设置地暖开关--场景
     */
    fun getFHOnOffScene(state: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_FH_ONOFF
        VendorFLKFromPl.short2byteBE(state.toShort(), command, 1)
        return command
    }


    /**
     * 设置地暖的温度
     * [temp] 10℃~32℃之间
     */
    fun setFHTemperature(temp: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_FH_TEMPERATURE
        VendorFLKFromPl.short2byteBE((temp * 10).toShort(), command, 2)
        return command
    }

    /**
     * 设置地暖的温度 -- 场景
     */
    fun getFHTemperatureScene(temp: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_FH_TEMPERATURE
        VendorFLKFromPl.short2byteBE((temp * 10).toShort(), command, 1)
        return command
    }


    /**
     * 设置地暖保护温度
     * [temp] 1℃~5℃之间
     */
    fun setFHProtectionTemperature(temp: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_FH_PROTECT_TEMP
        VendorFLKFromPl.short2byteBE((temp * 10).toShort(), command, 2)
        return command
    }

    /**
     * 设置地暖保护温度--场景
     */
    fun getFHProtectionTemperatureScene(temp: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_FH_PROTECT_TEMP
        VendorFLKFromPl.short2byteBE((temp * 10).toShort(), command, 1)
        return command
    }

    /**
     * 设置地暖继电器的控制模式
     * (地暖的优先级低)
     * @param relay   继电器    1:一路双线阀或一路三线阀（一控）默认
     *                         2:一路三线阀（二控）
     */
    fun setFHRelay(relay:Int):ByteArray{
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_FH_RELAY
        command[2] = 0x01
        command[3] = relay.to2ByteL()[0]
        return command
    }

    /**
     * 设置地暖继电器的控制模式 -- 场景
     */
    fun getFHRelayScene(relay:Int):ByteArray{
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_FH_RELAY
        command[1] = 0x01
        command[2] = relay.to2ByteL()[0]
        return command
    }

    /**
     * 设置地暖继电器无效化
     */
    fun setFHRelayInvalid():ByteArray{
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_FH_RELAY
        command[2] = 0x00
        command[3] = 0x00
        return command
    }

    /**
     * 设置地暖继电器无效化
     */
    fun getFHRelayInvalidScene():ByteArray{
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_FH_RELAY
        command[1] = 0x00
        command[2] = 0x00
        return command
    }


    /**
     * 设置新风开关
     * @param state 0是关机  1是开机
     */
    fun setFAOnOff(state: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_FA_ONOFF
        VendorFLKFromPl.short2byteBE(state.toShort(), command, 2)
        return command
    }

    /**
     * 设置新风开关  -- 场景
     */
    fun getFAOnOffScene(state: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_FA_ONOFF
        VendorFLKFromPl.short2byteBE(state.toShort(), command, 1)
        return command
    }


    /**
     * 设置新风风速
     * @param speed 01低速，02中速，03高速
     */
    fun setFASpeed(speed: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_FA_SPEED
        VendorFLKFromPl.short2byteBE(speed.toShort(), command, 2)
        return command
    }

    /**
     * 设置新风风速 - 场景
     */
    fun getFASpeedScene(speed: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_FA_SPEED
        VendorFLKFromPl.short2byteBE(speed.toShort(), command, 1)
        return command
    }




    /**
     * 设置新风继电器的控制模式
     * (地暖的优先级低)
     * @param relay   继电器    0:无效（默认，继电器不受控）
     *                         1:有效（寄存器 0016 的寄存器数据无效时）
     */
    fun setFARelay(relay:Int):ByteArray{
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_FA_RELAY
        command[2] = 0x00
        command[3] = relay.to2ByteL()[0]
        return command
    }


    /**
     * 设置新风继电器的控制模式 -- 场景
     */
    fun getFARelayScene(relay:Int):ByteArray{
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_FA_RELAY
        command[1] = 0x00
        command[2] = relay.to2ByteL()[0]
        return command
    }



    /**
     * 环境参数
     * 温度补偿（实时温度修正）
     * -9℃ ~ 9℃ 整数位有效
     * 00H ~ 09H 代表  0℃ ~ 9℃
     * F7H ~ FFH 代表  -9℃ ~ -1℃
     * @param temp 修正温度的值 范围: -9 ~ 9
     * [temp]:(小 ---->> 大) [-9 , 9]
     * -9 ==> F7    -8 ==> F8    -7 ==> F9    -6 ==> FA    -5 ==> FB    -4 ==> FC    -3 ==> FD    -2 ==> FE    -1 ==> FF
     *  0 ==> 00     1 ==> 01     2 ==> 02     3 ==> 03     4 ==> 04     5 ==> 05     6 ==> 06     7 ==> 07     8 ==> 08    9 ==> 09
     */
    fun setCompTemperature(temp: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_COMP_TEMP
        VendorFLKFromPl.short2byteBE((temp * 10).toShort(), command, 2)

        return command
    }

    /**
     * 环境参数
     * 温度补偿（实时温度修正） -- 场景
     */
    fun getCompTemperatureScene(temp: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_COMP_TEMP
        VendorFLKFromPl.short2byteBE((temp * 10).toShort(), command, 1)

        return command
    }


    /**
     * 背光休眠时间
     * @param level 只有7个等级
     *        0:30秒
     *        1:1分钟
     *        2:2分钟
     *        3:3分钟
     *        4:4分钟
     *        5:5分钟
     *        6:不休眠
     */
    fun setBgDuration(level: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_BG_DURATION
        when (level) {
            0 -> {
                VendorFLKFromPl.short2byteBE(30, command, 2)
            }
            1, 2, 3, 4, 5 -> {
                VendorFLKFromPl.short2byteBE((level * 60).toShort(), command, 2)
            }
            else -> {
                VendorFLKFromPl.short2byteBE(65535.toShort(), command, 2)
            }
        }
        return command
    }

    /**
     * 背光休眠时间 -- 场景
     */
    fun getBgDurationScene(level: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_BG_DURATION
        when (level) {
            0 -> {
                VendorFLKFromPl.short2byteBE(30, command, 1)
            }
            1, 2, 3, 4, 5 -> {
                VendorFLKFromPl.short2byteBE((level * 60).toShort(), command, 1)
            }
            else -> {
                VendorFLKFromPl.short2byteBE(65535.toShort(), command, 1)
            }
        }
        return command
    }


    /**
     * 背光亮度
     * @param keyLightness 唤醒时，按键灯的亮度，值：35 45 55 65 75 85
     * @param bgLightness  屏幕的亮度，值：35 45 55 65 75 85
     */
    fun setLightness(keyLightness: Int, bgLightness: Int): ByteArray {
        val command = ByteArray(4)
        command[0] = VendorFLKFromPl.TC_SET
        command[1] = VendorFLKFromPl.TYPE_BG_LIGHTNESS
        command[2] = (keyLightness and 0xff).toByte()
        command[3] = (bgLightness and 0xff).toByte()
        // VendorFLKFromPl.short2byteBE(lowH.toShort(), command, 2)
        return command
    }

    /**
     * 背光亮度-- 场景
     */
    fun getLightnessScene(keyLightness: Int, bgLightness: Int): ByteArray {
        val command = ByteArray(3)
        command[0] = VendorFLKFromPl.TYPE_BG_LIGHTNESS
        command[1] = (keyLightness and 0xff).toByte()
        command[2] = (bgLightness and 0xff).toByte()
        // VendorFLKFromPl.short2byteBE(lowH.toShort(), command, 2)
        return command
    }


    /**
     * 接收通知的时间限制
     *
     */
    fun getNotifyTime(): ByteArray = byteArrayOf(VendorFLKFromPl.TC_GET_NOTIFY)


    /**
     * 设置多少秒内空调状态变化会主动往外发消息
     * @param time [INT_MAX_POWER_OF_TWO]
     */
    fun setNotifyTime(time: Int): ByteArray {
        val command = ByteArray(5)
        command[0] = VendorFLKFromPl.TC_SET_NOTIFY
        val timeArray = time.to4ByteL()
        System.arraycopy(timeArray, 0, command, 1, timeArray.size)
        return command
    }

}